package com.czq.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czq.dao.CategoryDao;
import com.czq.execption.BusinessEXception;
import com.czq.pojo.CateGory;
import com.czq.pojo.Dish;
import com.czq.pojo.Setmeal;
import com.czq.service.CategoryService;
import com.czq.service.DishService;
import com.czq.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CategoryServiceImpl   extends ServiceImpl<CategoryDao, CateGory>  implements CategoryService {
   @Autowired
   private DishService dishService;
   @Autowired
   private SetmealService setmealService;


    @Override
    public void deleteByid(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int dishes = dishService.count(queryWrapper);
        if (dishes>0){
            throw  new BusinessEXception("该菜品关联菜系不能删除");
        }
        LambdaQueryWrapper<Setmeal> query=new LambdaQueryWrapper<>();
        query.eq(Setmeal::getCategoryId,id);
        int setmeals = setmealService.count(query);
        if (setmeals>0){
            throw  new BusinessEXception("该套餐关联菜系不能删除");
        }
        super.removeById(id);

    }
}
